package org.farrell.EOSCalc;

import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TextView;

public class PropDisplay extends TabActivity {
	
	TabHost tabHost;
	
	public static final Bundle results = new Bundle();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
        	results.putAll(getIntent().getExtras());
        } else {
        	results.putAll(savedInstanceState);
        }
        
        switch (results.getInt(CubicEOS.KEY_ROOTS)){
        case 1:
        	setContentView(R.layout.prop_display_single);
        	break;
        	
        case 2:
        	setContentView(R.layout.prop_display);
        	break;
        }
        
        setTitle(R.string.results);
        
        tabHost = getTabHost();
        
        populateTable(results);
        
        tabHost.setCurrentTab(0);
    }
    
    public void populateTable(Bundle results) {
    	//Unpack the bundle
    	double pressure = results.getDouble(CubicEOS.KEY_PRES)*0.001;
    	double temperature = results.getDouble(CubicEOS.KEY_TEMP);
    	double[] z = results.getDoubleArray(CubicEOS.KEY_Z);
    	double[] v = results.getDoubleArray(CubicEOS.KEY_VOL);
    	double[] f = results.getDoubleArray(CubicEOS.KEY_FUG);
    	double[] h = results.getDoubleArray(CubicEOS.KEY_ENTHALPY);
    	double[] s = results.getDoubleArray(CubicEOS.KEY_ENTROPY);
    	double[] g = results.getDoubleArray(CubicEOS.KEY_GIBBS);
    	
    	switch (results.getInt(CubicEOS.KEY_ROOTS)) {
    	case 1:
    		
    		tabHost.addTab(tabHost.newTabSpec("pdl_view").setIndicator("Single Phase").setContent(R.id.pdl_view));
        	
        	TextView mPressureText = (TextView) findViewById(R.id.pdl_pres);
        	mPressureText.setText(Double.toString(pressure));
        	
        	TextView mTemperatureText = (TextView) findViewById(R.id.pdl_temp);
        	mTemperatureText.setText(Double.toString(temperature));
        	
        	TextView mZText = (TextView) findViewById(R.id.pdl_comp);
        	mZText.setText(Double.toString(z[0]));
        	
        	TextView mVText = (TextView) findViewById(R.id.pdl_vol);
        	mVText.setText(Double.toString(v[0]));
        	
        	TextView mfText = (TextView) findViewById(R.id.pdl_fug);
        	mfText.setText(Double.toString(f[0]*0.001));
        	
        	TextView mHText = (TextView) findViewById(R.id.pdl_enthalpy);
        	mHText.setText(Double.toString(h[0]));
        	
        	TextView mSText = (TextView) findViewById(R.id.pdl_entropy);
        	mSText.setText(Double.toString(s[0]));
        	
        	TextView mGText = (TextView) findViewById(R.id.pdl_gibb);
        	mGText.setText(Double.toString(g[0]));
        	
    		break;
    	
        case 2:
        	tabHost.addTab(tabHost.newTabSpec("pdl_view").setIndicator("Liquid Phase").setContent(R.id.pdl_view));
        	tabHost.addTab(tabHost.newTabSpec("pdv_view").setIndicator("Vapour Phase").setContent(R.id.pdv_view));
        	
        	TextView mPressure2Text = (TextView) findViewById(R.id.pdv_pres);
        	mPressure2Text.setText(Double.toString(pressure));
        	
        	TextView mTemperature2Text = (TextView) findViewById(R.id.pdv_temp);
        	mTemperature2Text.setText(Double.toString(temperature));
        	
        	TextView mZ2Text = (TextView) findViewById(R.id.pdv_comp);
        	mZ2Text.setText(Double.toString(z[1]));
        	
        	TextView mV2Text = (TextView) findViewById(R.id.pdv_vol);
        	mV2Text.setText(Double.toString(v[1]));
        	
        	TextView mf2Text = (TextView) findViewById(R.id.pdv_fug);
        	mf2Text.setText(Double.toString(f[1]*0.001));
        	
        	TextView mH2Text = (TextView) findViewById(R.id.pdv_enthalpy);
        	mH2Text.setText(Double.toString(h[1]));
        	
        	TextView mS2Text = (TextView) findViewById(R.id.pdv_entropy);
        	mS2Text.setText(Double.toString(s[1]));
        	
        	TextView mG2Text = (TextView) findViewById(R.id.pdv_gibb);
        	mG2Text.setText(Double.toString(g[1]));
        	
        	TextView mPressure1Text = (TextView) findViewById(R.id.pdl_pres);
        	mPressure1Text.setText(Double.toString(pressure));
        	
        	TextView mTemperature1Text = (TextView) findViewById(R.id.pdl_temp);
        	mTemperature1Text.setText(Double.toString(temperature));
        	
        	TextView mZ1Text = (TextView) findViewById(R.id.pdl_comp);
        	mZ1Text.setText(Double.toString(z[0]));
        	
        	TextView mV1Text = (TextView) findViewById(R.id.pdl_vol);
        	mV1Text.setText(Double.toString(v[0]));
        	
        	TextView mf1Text = (TextView) findViewById(R.id.pdl_fug);
        	mf1Text.setText(Double.toString(f[0]*0.001));
        	
        	TextView mH1Text = (TextView) findViewById(R.id.pdl_enthalpy);
        	mH1Text.setText(Double.toString(h[0]));
        	
        	TextView mS1Text = (TextView) findViewById(R.id.pdl_entropy);
        	mS1Text.setText(Double.toString(s[0]));
        	
        	TextView mG1Text = (TextView) findViewById(R.id.pdl_gibb);
        	mG1Text.setText(Double.toString(g[0]));
        	
        	break;
        }
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putAll(results);
        
    }
    
}
